<?php

namespace app\api\controller;

use app\common\controller\Base;
use app\common\lib\Xf;
use app\common\model\Anwser;
use app\common\model\Question;
use app\common\model\UploadFiles as UploadFilesModel;
use app\common\model\User as UserModel;
use think\Request;

class Index extends Base
{
    /**
     * 登录用户
     * @var UserModel
     */
    public $user;

    public function initialize()
    {
        header('Content-Type:application/json;charset=utf-8');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: *');
        header('Access-Control-Allow-Headers: *');
        header('Access-Control-Allow-Credentials: true');
        if (request()->action() != 'login') {
            $phone = $_REQUEST['phone'];
            $this->user = UserModel::where(['phone' => $phone])->find();
            if (!$this->user) {
                return json(['msg' => '请登录', 'code' => 401]);
            }
        }
    }

    /**
     * 登录
     *
     * @param Request $request
     * @return void
     * @author Ymob
     * @datetime 2021-04-24 13:54:15
     */
    public function login(Request $request)
    {
        $data = $request->post();

        if (!UserModel::where($data)->find() && UserModel::create($data)) {
            return json(['msg' => '失败', 'code' => 401]);
        } else {
            return json(['code' => 200]);
        }
    }

    /**
     * 获取问题
     */
    public function question()
    {
        $data = Question::getQuestion($this->user);
        if ($data != null) {
            $data['poem'] = $data->poem;
            $data['first'] = $data->first;
            $data['second'] = $data->second;
            $data['poem']['content_html'] = nl2br($data['poem']['content']);
        }
        return json([
            'code' => 200,
            'data' => $data,
        ]);
    }

    /**
     * 回答问题
     */
    public function anwser(Request $request)
    {
        $res = UploadFilesModel::upload('file');
        if ($res['code']) {
            $cli = new Xf;
            $question_id = $request->param('question_id');
            $asrRes = $cli->asr(\public_path() . $res['file']);
            $data = [
                'user_id' => $this->user->id,
                'question_id' => $question_id,
                'record' => $res['file'],
                'correct' => 0,
                'text' => '',
            ];
            $this->user->last_question_id = $question_id;
            $this->user->save();
            if ($asrRes) {
                $question = Question::find($question_id);
                $anwser = $question->type ? $question->first : $question->second;
                $correct = strpos($asrRes, $anwser) === false ? 2 : 1;
                $data['text'] = $asrRes;
                $data['correct'] = $correct;
                if (Anwser::create($data)) {
                    $this->user->anwser((int) $correct == 1);
                    return json([
                        'data' => [
                            'error' => 0,
                            'success' => $correct,
                        ],
                        'code' => 200,
                    ]);
                } else {
                    $msg = '数据库写入失败。';
                }
            } else {
                $msg = '识别失败。';
                Anwser::create($data);
            }
        } else {
            $msg = '录音上传失败';
        }
        return json([
            'data' => ['error' => 1, 'msg' => $msg],
            'code' => 200,
        ]);
    }

    /**
     * 
     */
    public function userinfo()
    {
        return json([
            'code' => 200,
            'data' => [
                'num' => $this->user->num,
                'correct' => $this->user->correct,
                'rate' => $this->user->rate,
            ],
        ]);
    }
}
